package cn.xinfei.xdecision.conf;

import cn.xinfei.xdecision.listener.IXListener;
import cn.xinfei.xdecision.listener.event.CustomerStartedEvent;
import cn.xinfei.xdecision.nacos.CustomerNacosAutoServiceRegistration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.context.event.ApplicationStartingEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;

import java.util.List;


@Slf4j
@Configuration
public class ApplicationEventListener implements ApplicationListener<ApplicationEvent>, ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Autowired
    private CustomerNacosAutoServiceRegistration customerNacosAutoServiceRegistration;

    @Autowired
    private List<IXListener> ixListeners;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    //    @Autowired
//    private RabbitListenerEndpointRegistry rabbitListenerEndpointRegistry;
    @Override
    public void onApplicationEvent(ApplicationEvent event) {
//        ApplicationStartingEvent//启动开始的时候执行的事件
//        ApplicationEnvironmentPreparedEvent//上下文创建之前运行的事件
//        ApplicationContextInitializedEvent//
//        ApplicationPreparedEvent//上下文创建完成，注入的bean还没加载完成
//        ContextRefreshedEvent//上下文刷新
//        ServletWebServerInitializedEvent//web服务器初始化
//        ApplicationStartedEvent//
//        ApplicationReadyEvent//启动成功
//        ApplicationFailedEvent//在启动Spring发生异常时触发
        switch (event.getClass().getSimpleName()) {
            case "ApplicationStartingEvent":
                log.info("启动开始的时候执行的事件");
                break;
            case "ApplicationEnvironmentPreparedEvent":
                log.info("上下文创建之前运行的事件");
                break;
            case "ApplicationContextInitializedEvent":
                log.info("上下文初始化");
                ixListeners.stream().filter(l->l.eventName().equals(ApplicationStartingEvent.class.getSimpleName())).forEach(i -> i.after());
                break;
            case "ServletWebServerInitializedEvent":
                log.info("web服务器初始化");
                break;
            case "ApplicationPreparedEvent":
                log.info("上下文创建完成，注入的bean还没加载完成：ApplicationPreparedEvent");
                break;
            case "ContextRefreshedEvent":
                log.info("上下文刷新");
//                rabbitListenerEndpointRegistry.start();
                break;
            case "ApplicationStartedEvent":
                log.info("ApplicationStartedEvent");
                break;
            case "ApplicationReadyEvent":
                //nacos的一定要最后执行！！！
                log.info("启动成功");
                customerNacosAutoServiceRegistration.delayStart();
                applicationContext.publishEvent(new CustomerStartedEvent("XStarted"));
                //单元测试时不执行
                break;
            case "ApplicationFailedEvent":
                log.info("在启动Spring发生异常时触发");
                break;
        }
    }

}
